
************************************************************************************
*** Cost of Compliance, Autocratic Time Horizon, and Investment Treaty Formation ***
***           Jia Chen and Fangjin Ye, Political Research Quarterly              ***
***                              Replication file (Stata 12)                     ***
************************************************************************************


///open the stata data file "home_full_host.dta"

***************************************
****** variable construction ********
***************************************
xtset dyad_id year
gen lPOLCONV_diff = l.POLCONV_diff
gen lpolity2_diff = l.polity2_diff
gen lgdp_sum_ln = l.gdp_sum_ln
gen lgdp_diff_square_ln=l.gdp_diff_square_ln
gen lgdp_capita_diff_ln=l.gdp_capita_diff_ln 
gen lcomlang_off= l.comlang_off 
gen lcolony=l.colony
gen ldist_ln=l.dist_ln
gen llji_diff=l.lji_diff
gen lpta_dyad=l.pta_dyad
gen llnten =l.lnten
gen lgwf_party=l.gwf_party
gen lgwf_military=l.gwf_military
gen lgwf_monarchy=l.gwf_monarchy
gen lgwf_personal=l.gwf_personal
gen llparty_CGV =l.lparty_CGV
gen ltotdur1ny_HT =l.totdur1ny_HT
gen ltenure1ny_HT =l.tenure1ny_HT
gen lkaopen=l.kaopen
gen linflow_fdi_gdp_host=l.inflow_fdi_gdp_host

gen abs_fdi_outflow_level = abs(fdi_outflow_million*10^6)
gen log_fdi_outflow_level = log(abs_fdi_outflow_level+1)
replace log_fdi_outflow_level=-log_fdi_outflow_level if fdi_outflow_million<0
gen llog_fdi_outflow_level=l.log_fdi_outflow_level

gen abs_fdi_inflow_million_host = abs(fdi_inflow_million_host*10^6)
gen log_fdi_inflow_million_host = log(abs_fdi_inflow_million_host+1)
replace log_fdi_inflow_million_host=-log_fdi_inflow_million_host if fdi_inflow_million_host<0
gen llog_fdi_inflow_million_host=l.log_fdi_inflow_million_host

gen lcumBIT_claims_host=l.cumBIT_claims_host
gen lcumRBIT_claims_host=l.cumRBIT_claims_host
gen lglob_cum_claim=l.glob_cum_claim
gen lBITclaim_host=l.BITclaim_host
gen lRBITclaim_host=l.RBITclaim_host
gen lBITclaim_home=l.BITclaim_home
gen ldyadBITclaim = 1 if lBITclaim_home==1 | lBITclaim_host==1
replace ldyadBITclaim =0 if lBITclaim_home==0 & lBITclaim_host==0
   
gen monarchy_HT=. 
replace monarchy_HT=1 if regime1ny_HT==1
replace monarchy_HT=0 if regime1ny_HT==2|regime1ny_HT==3|regime1ny_HT==4|regime1ny_HT==9

gen military_HT=.
replace military_HT=1 if regime1ny_HT==2
replace military_HT=0 if regime1ny_HT==1|regime1ny_HT==3|regime1ny_HT==4|regime1ny_HT==9

gen oneparty_HT=. 
replace oneparty_HT=1 if regime1ny_HT==3
replace oneparty_HT=0 if regime1ny_HT==1|regime1ny_HT==2|regime1ny_HT==4|regime1ny_HT==9

gen multiparty_HT=. 
replace multiparty_HT=1 if regime1ny_HT==4
replace multiparty_HT=0 if regime1ny_HT==1|regime1ny_HT==2|regime1ny_HT==3|regime1ny_HT==9


gen noparty_HT=.
replace noparty_HT=1 if regime1ny_HT==9
replace noparty_HT=0 if regime1ny_HT==1|regime1ny_HT==2|regime1ny_HT==3|regime1ny_HT==4

gen civilian_HT=.
replace civilian_HT=1 if regime1ny_HT==3|regime1ny_HT==4|regime1ny_HT==9
replace civilian_HT=0 if regime1ny_HT==1|regime1ny_HT==2

gen lmonarchy_HT = l.monarchy_HT
gen lmilitary_HT=l.military_HT
gen loneparty_HT=l.oneparty_HT
gen lmultiparty_HT=l.multiparty_HT
gen lnoparty_HT=l.noparty_HT
gen lcivilian_HT =l.civilian_HT
gen lexecrlc=l.execrlc


gen halfdecade1 = 0
replace halfdecade1=1 if year>=1955 & year<1960
gen halfdecade2 = 0
replace halfdecade2=1 if year>=1960 & year<1965
gen halfdecade3 = 0
replace halfdecade3=1 if year>=1965 & year<1970
gen halfdecade4 = 0
replace halfdecade4=1 if year>=1970 & year<1975
gen halfdecade5 = 0
replace halfdecade5=1 if year>=1975 & year<1980
gen halfdecade6 = 0
replace halfdecade6=1 if year>=1980 & year<1985
gen halfdecade7 = 0
replace halfdecade7=1 if year>=1985 & year<1990
gen halfdecade8 = 0
replace halfdecade8=1 if year>=1990 & year<1995
gen halfdecade9 = 0
replace halfdecade9=1 if year>=1995 & year<2000
gen halfdecade10 = 0
replace halfdecade10=1 if year>=2000 & year<2005
gen halfdecade11 = 0
replace halfdecade11=1 if year>=2005 & year<2010
gen halfdecade12 = 0
replace halfdecade12=1 if year>=2010

xtset dyad_id year

tab ccode_home, gen(homecountry_dummy)

global control "lPOLCONV_diff lpolity2_diff lgdp_sum_ln lgdp_diff_square_ln lgdp_capita_diff_ln lcomlang_off lcolony dist_ln llji_diff lgdp_growth_wdi_host llntrade_home llntrade_host loutflow_fdi_gdp_home lpta_dyad lglob_ann_sign lregion_cum_sign coldwar"

stset year, id(dyad_id) failure(sign) origin(time atrisk1958)


label var lfail_wright "Pr(Fail)"
label var lfail_update_wdi "Pr(Fail)"
label var lPOLCONV_diff "POLCON Diff."
label var lpolity2_diff "Polity2 Diff."
label var lgdp_sum_ln "GDP Sum"
label var lgdp_diff_square_ln "GDP Diff. (squared)"
label var lgdp_capita_diff_ln "GDP pc Diff."
label var lcomlang_off "Common Language"
label var lcolony  "Colonial Linkage"
label var dist_ln "Geo. Distance"
label var llji_diff  "Judicial Diff."
label var lgdp_growth_wdi_host "Host Growth"
label var llntrade_home "Home Trade"
label var llntrade_host "Host Trade"
label var loutflow_fdi_gdp_home "Home FDI Outflow"
label var lpta_dyad "PTA Dummy"
label var lglob_ann_sign "Global BITs Signed"
label var lregion_cum_sign "Regional BITs Signed"
label var coldwar "Cold War Dummy"
label var lgwf_party "Party-based Regime"
label var llnten "Leadership Tenure"


***************************************
****** main tables and figures ********
***************************************

******Table 1
stcox lfail_wright $control if auto_GWF==1, vce(robust) nohr

stcox lfail_update_wdi $control if auto_GWF==1, vce(robust) nohr

stcox lfail_update_wdi $control if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln lgdp_growth_wdi_host llntrade_home lregion_cum_sign)

stcox c.lfail_update_wdi##c.ldyadBITclaim  $control if auto_GWF==1, vce(robust) nohr tvc(ldyadBITclaim lPOLCONV_diff lgdp_sum_ln lgdp_growth_wdi_host llntrade_home loutflow_fdi_gdp_home lpta_dyad)

stcox lfail_update_wdi homecountry_dummy* $control if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_growth_wdi_host lregion_cum_sign)

stcox lfail_update_wdi homecountry_dummy* halfdecade* $control if auto_GWF==1, vce(robust) nohr tvc(halfdecade9 halfdecade10 lPOLCONV_diff lgdp_growth_wdi_host lregion_cum_sign)

stcox lfail_update_wdi $control if auto_GWF==1, nohr shared(dyad_id) 

******Table 2
stcox lgwf_party $control if auto_GWF==1, vce(robust) nohr 

stcox lgwf_party $control if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln llntrade_home lregion_cum_sign)

stcox lgwf_party $control homecountry_dummy* halfdecade* if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln llntrade_home lregion_cum_sign)

stcox lcivilian_HT $control if auto_GWF==1, vce(robust) nohr 

stcox lcivilian_HT $control if auto_GWF==1, vce(robust) nohr tvc(lgdp_sum_ln llji_diff llntrade_home loutflow_fdi_gdp_home lpta_dyad lregion_cum_sign)

stcox lcivilian_HT $control homecountry_dummy* halfdecade* if auto_GWF==1, vce(robust) nohr tvc(lgdp_sum_ln llji_diff llntrade_home loutflow_fdi_gdp_home lpta_dyad lregion_cum_sign)

stcox  ltotdur1ny_HT $control if auto_GWF==1, vce(robust) nohr 

stcox  ltotdur1ny_HT $control if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln llntrade_home loutflow_fdi_gdp_home lpta_dyad)

stcox  ltotdur1ny_HT $control homecountry_dummy* halfdecade* if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln llntrade_home loutflow_fdi_gdp_home lpta_dyad)

****Figure 1
///open the stata data file "scatterplot.dta"
**[a] BITs and FDI inflows(level)   
twoway (scatter bits lnfdi1 if year == 2008 & pr != . , msymbol(none)  mlabel(wdicode) mlabsize(small) mlabcolor(gs4) mlabposition(0) ) ///
       (scatter bits lnfdi1 if year == 2008 & wdicode == "EGY", msymbol(none) mlabcolor(red) mlabel(wdicode) mlabposition(0) mlabsize(small)) ///
       (scatter bits lnfdi1 if year == 2008 & wdicode == "SDN", msymbol(none) mlabcolor(red) mlabel(wdicode) mlabposition(0) mlabsize(small)) ///
       (lfit bits lnfdi1 if year == 2008 & pr != . , lpattern(dash) lcolor(gs10) ), ///
       legend(off) xtitle("FDI Inflows (log)", size(medsmall)) ytitle(BITs Signed, size(medsmall)) aspectratio(.9)
	   
**[b] BITs and FDI stock(level)		   
twoway (scatter bits lnfdistk if year == 2008 & pr != .  , msymbol(none)  mlabel(wdicode) mlabsize(small) mlabcolor(gs4) mlabposition(0) )  ///
       (scatter bits lnfdistk if year == 2008 & wdicode == "EGY" , msymbol(none)  mlabel(wdicode) mlabsize(small) mlabcolor(red) mlabposition(0) )  ///
       (scatter bits lnfdistk if year == 2008 & wdicode == "SDN" , msymbol(none)  mlabel(wdicode) mlabsize(small) mlabcolor(red) mlabposition(0) )  ///
       (lfit bits lnfdistk if year == 2008 & pr != .  , lpattern(dash) lcolor(gs10)), ///
	   legend(off) xtitle("FDI Stocks (log)", size(medsmall)) ytitle(BITs Signed, size(medsmall))  aspectratio(.9)

****Figure 2: (based on model 2) 
quietly stcox lfail_update_wdi $control if auto_GWF==1, vce(robust) nohr 
stcurve, survival range(0 60) /// 
		 at1(lfail_update_wdi=0 ) at2( lfail_update_wdi=.1094861) lcolor(gs0 gs12) /// 
		 ytitle(Percentage of BITs not signed, margin(small)  size(medsmall)) xtitle(Years under analysis ({it:t}), margin(small)) title(" " , size(small)) ///
		 legend(order(2 "Regime Risk (High)" 1 "Regime Risk (Low)") cols(1) pos(1) ring(0) size(small))

****percentage change of hazard rate based on simulation (based on model 2)
stcox lfail_update_wdi $control if auto_GWF==1, vce(robust) nohr 

set seed 12345678
drawnorm SN_b1-SN_b18, n(10000) means(e(b)) cov(e(V)) clear

generate x_betahat0 = SN_b1*0
generate x_betahat1 = SN_b1*.1094861
    gen exp0=exp(x_betahat0)
    gen exp1=exp(x_betahat1)
    gen risk=((exp1-exp0)/exp0)*100
   
    egen riskhat=mean(risk)
    
    _pctile risk, p(2.5,97.5)
    
    scalar h1= r(r1)
    scalar h2= r(r2)

    dis h1
    dis h2
    sum riskhat

**percentage change of hazard rate based on simulation (based on model 8)
stcox lgwf_party $control if auto_GWF ==  1, vce(robust) nohr

set seed 12345678
drawnorm SN_b1-SN_b18, n(10000) means(e(b)) cov(e(V)) clear

generate x_betahat0 = SN_b1*0
generate x_betahat1 = SN_b1*1
    gen exp0=exp(x_betahat0)
    gen exp1=exp(x_betahat1)
    gen risk=((exp1-exp0)/exp0)*100
   
    egen riskhat=mean(risk)
    
    _pctile risk, p(2.5,97.5)
    
    scalar h1= r(r1)
    scalar h2= r(r2)

    dis h1
    dis h2
    sum riskhat

***model 11
stcox lcivilian_HT $control if auto_GWF==1, vce(robust) nohr 

set seed 12345678
drawnorm SN_b1-SN_b18, n(10000) means(e(b)) cov(e(V)) clear

generate x_betahat0 = SN_b1*0
generate x_betahat1 = SN_b1*1
    gen exp0=exp(x_betahat0)
    gen exp1=exp(x_betahat1)
    gen risk=((exp1-exp0)/exp0)*100
   
    egen riskhat=mean(risk)
    
    _pctile risk, p(2.5,97.5)
    
    scalar h1= r(r1)
    scalar h2= r(r2)

    dis h1
    dis h2
    sum riskhat



***model 14
stcox  ltotdur1ny_HT $control if auto_GWF==1, vce(robust) nohr

set seed 12345678
drawnorm SN_b1-SN_b18, n(10000) means(e(b)) cov(e(V)) clear

generate x_betahat0 = SN_b1*9.88196
generate x_betahat1 = SN_b1*36.58512
    gen exp0=exp(x_betahat0)
    gen exp1=exp(x_betahat1)
    gen risk=((exp1-exp0)/exp0)*100
   
    egen riskhat=mean(risk)
    
    _pctile risk, p(2.5,97.5)
    
    scalar h1= r(r1)
    scalar h2= r(r2)

    dis h1
    dis h2
    sum riskhat


****Online Appendix 
**Table A1: summary statistics
stcox lfail_update_wdi $control if auto_GWF==1, vce(robust) nohr
keep if e(sample)
keep lfail_update_wdi lgwf_party lcivilian_HT ltotdur1ny_HT lPOLCONV_diff lpolity2_diff lgdp_sum_ln lgdp_diff_square_ln lgdp_capita_diff_ln lcomlang_off lcolony dist_ln llji_diff lgdp_growth_wdi_host llntrade_home llntrade_host loutflow_fdi_gdp_home lpta_dyad lglob_ann_sign lregion_cum_sign coldwar
sum lfail_update_wdi lgwf_party lcivilian_HT ltotdur1ny_HT lPOLCONV_diff lpolity2_diff lgdp_sum_ln lgdp_diff_square_ln lgdp_capita_diff_ln lcomlang_off lcolony dist_ln llji_diff lgdp_growth_wdi_host llntrade_home llntrade_host loutflow_fdi_gdp_home lpta_dyad lglob_ann_sign lregion_cum_sign coldwar

**Table A2: Predicting Autocratic Regime Failure (updated to 2008)
///open the stata data file "wright_update.dta"
logit end lngdp_capita_wdi gdp_capita_growth_2yr_ave_wdi civilwar islam2_ave  coldwar /*
*/ regimetype_dum6 regimetype_dum2 regimetype_dum4 regimetype_dum8 regimetype_dum3 regimetype_dum7 regimetype_dum9 /*
*/ ssafrica nafrica meast casia ceeurope easia samerica weurope  /*
*/ _spline1 _spline2 _spline3 _spline4, cluster(cowcode)

predict xb, xb
predict pr
ren pr fail_update_wdi

**Table A3: control for ideology/learning effect
gen lrightwing = 1 if lexecrlc ==1
replace lrightwing = 0 if lexecrlc < 1 & lexecrlc~=-999 | lexecrlc > 1 

gen lleftwing = 1 if lexecrlc ==3
replace lleftwing = 0 if lexecrlc <3 & lexecrlc~=-999

stcox lfail_update_wdi lrightwing lleftwing $control if auto_GWF==1, vce(robust) nohr tvc(lleftwing lPOLCONV_diff lgdp_sum_ln lgdp_growth_wdi_host llntrade_home lregion_cum_sign)

stcox lfail_update_wdi lglob_cum_claim  $control if auto_GWF==1, vce(robust) nohr tvc(lPOLCONV_diff lgdp_sum_ln lgdp_growth_wdi_host llntrade_home loutflow_fdi_gdp_home)

stcox lfail_update_wdi ldyadBITclaim   $control if auto_GWF==1, vce(robust) nohr tvc(ldyadBITclaim lPOLCONV_diff lgdp_sum_ln lgdp_growth_wdi_host llntrade_home loutflow_fdi_gdp_home lpta_dyad)

**Table B1 and Figure B1
///open the stata data file "pr_isds.dta"
global cov "pr cim cum_n_s cum_n_s_sq global lnfdistk fdigdp"

zinb num_cases $cov lagddv, inflate(nobits_ye) exposure(year) cluster(cow) nolog  irr

coefplot,drop(_cons lagddv) xline(1) eform aspectratio(.6) base title("Incidence Rate Ratio with 95% CI", size(medium))





